<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item label="领队名称" prop="teamName">
        <el-input v-model="dataForm.teamName" placeholder="领队名称"></el-input>
      </el-form-item>
      <el-form-item label="单位" prop="company">
        <el-input v-model="dataForm.company" placeholder="单位"></el-input>
      </el-form-item>
      <el-form-item label="税号" prop="companyNo">
        <el-input v-model="dataForm.companyNo" placeholder="税号"></el-input>
      </el-form-item>
      <el-form-item label="地区" prop="province">
        <el-select v-model="dataForm.province" placeholder="请选择省">
          <el-option label="人人" value="0"></el-option>
        </el-select>
        ~
        <el-select v-model="dataForm.city" placeholder="请选择市">
          <el-option label="人人" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="领队姓名" prop="leader">
        <el-input v-model="dataForm.leader" placeholder="领队姓名"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <ren-radio-group v-model="dataForm.sex" dict-type="gender"></ren-radio-group>
      </el-form-item>
      <el-form-item label="证件类别" prop="idType">
        <el-input v-model="dataForm.idType" placeholder="证件类别"></el-input>
      </el-form-item>
      <el-form-item label="身份证号" prop="idCard">
        <el-input v-model="dataForm.idCard" placeholder="身份证号"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="dataForm.phone" placeholder="手机号"></el-input>
      </el-form-item>
      <el-form-item label="头像" prop="header">
        <el-input v-model="dataForm.header" placeholder="头像"></el-input>
      </el-form-item>
      <el-form-item label="授权书" prop="empower">
        <el-input v-model="dataForm.empower" placeholder="授权书"></el-input>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts">
import { defineComponent, reactive } from "vue";
import baseService from "@/service/baseService";
import { useDebounce } from "@/utils/utils";
export default defineComponent({
  setup() {
    return reactive({
      visible: false,
      dataForm: {
        id: "",
        teamName: "",
        company: "",
        companyNo: "",
        province: "",
        city: "",
        leader: "",
        sex: "",
        idType: "",
        idCard: "",
        phone: "",
        header: "",
        empower: "",
        createTime: "",
        updateTime: ""
      }
    });
  },
  computed: {
    dataRule() {
      return {};
    }
  },
  created() {
    this.dataFormSubmitHandle = useDebounce(this.dataFormSubmitHandle);
  },
  methods: {
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataFormRef"].resetFields();
        if (this.dataForm.id) {
          this.getInfo();
        }
      });
    },
    // 获取信息
    getInfo() {
      baseService.get("/sp/spteam/" + this.dataForm.id).then((res) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        }
        this.dataForm = res.data;
      });
    },
    // 表单提交
    dataFormSubmitHandle() {
      this.$refs["dataFormRef"].validate((valid: boolean) => {
        if (!valid) {
          return false;
        }
        (!this.dataForm.id ? baseService.post : baseService.put)("/sp/spteam", this.dataForm).then((res) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.$message({
            message: this.$t("prompt.success"),
            type: "success",
            duration: 500,
            onClose: () => {
              this.visible = false;
              this.$emit("refreshDataList");
            }
          });
        });
      });
    }
  }
});
</script>
